package com.cloudcc.mobile.view.activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Window;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.util.Tools;
import com.cloudcc.mobile.view.fragment.ImageDetailFragment;
import com.cloudcc.mobile.weight.HackyViewPager;

public class ImagePagerActivity extends FragmentActivity {
	private static final String STATE_POSITION = "STATE_POSITION";
	public static final String EXTRA_IMAGE_INDEX = "image_index";
	public static final String EXTRA_IMAGE_URLS = "image_urls";
	public static final String EXTRA_IMAGE_FEEDFILES = "FeedFiles";
	private HackyViewPager mPager;
	private int pagerPosition;
	private TextView indicator;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		try {
			setContentView(R.layout.image_detail_pager);
			setupView(savedInstanceState);
			addListener();
		} catch (Exception e) {
			Tools.handle(e);
		}
	}

	private void setupView(Bundle savedInstanceState) {
		pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);
		String[] urls = getIntent().getStringArrayExtra(EXTRA_IMAGE_URLS);
		String[] ids=getIntent().getStringArrayExtra(EXTRA_IMAGE_FEEDFILES);
		mPager = (HackyViewPager) findViewById(R.id.pager);
		ImagePagerAdapter mAdapter = new ImagePagerAdapter(
				getSupportFragmentManager(), urls,ids);
		mPager.setAdapter(mAdapter);
		indicator = (TextView) findViewById(R.id.indicator);
		CharSequence text = getString(R.string.viewpager_indicator, pagerPosition+1, mPager
				.getAdapter().getCount());
		indicator.setText(text);
		if (savedInstanceState != null) {
			pagerPosition = savedInstanceState.getInt(STATE_POSITION);
		}
		mPager.setCurrentItem(pagerPosition);
	}

	private void addListener() {
		// 更新下标
		mPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			@Override
			public void onPageSelected(int arg0) {
				CharSequence text = getString(R.string.viewpager_indicator,
						arg0 + 1, mPager.getAdapter().getCount());
				indicator.setText(text);
			}
		});
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putInt(STATE_POSITION, mPager.getCurrentItem());
	}

	private class ImagePagerAdapter extends FragmentStatePagerAdapter {
		public String[] fileList;
		public String[] ids;
		public ImagePagerAdapter(FragmentManager fm, String[] fileList,String[] ids) {
			super(fm);
			this.fileList = fileList;
			this.ids = ids;
		}
		@Override
		public int getCount() {
			return fileList == null ? 0 : fileList.length;
		}
		@Override
		public Fragment getItem(int position) {
			String url = fileList[position];
			String id=ids[position];
			return ImageDetailFragment.newInstance(url,id);
		}

	}
}